<?php $this->headScript()->appendFile($this->JS_PATH . 'projectform.js'); ?>
<div id="MainContent">
    <div class="pageerrorcontainer-validator jscript-error-container">
        <div class="error-iterator"></div>
    </div>
    <?php if ($this->succesMessage != ""): ?>
        <div class='pagemcontainer'>
            <p class='pagemessage'>
                <img src="<?php echo $this->IMAGE_PATH; ?>icons/system/accept.gif" class="systemicon" alt="Success" title="Success" />
                <label class='errorlabel'><?php echo $this->succesMessage; ?></label>
            </p>
        </div>
    <?php endif; ?>			

    <?php if (count($this->errorMessageArray) > 0): ?>
        <div class="pageerrorcontainer">
            <?php foreach ($this->errorMessageArray as $erroValue): ?>
                <div class="pageoverflow">
                    <img src="<?php echo $this->IMAGE_PATH; ?>icons/system/stop.gif" class="systemicon" alt="Error" title="Error" />
                    <label class="errorlabel"><?php echo $erroValue; ?></label>
                </div>
            <?php endforeach; ?>				
        </div>
    <?php endif; ?>			
    <div class="clearb">&nbsp;</div>
    <div id="projectform"></div>
    <div class="pagecontainer pageoverflow">
        <div class="pageheader">
            Project dashboard
        </div>
        <div style="clear: both;"></div>
        <form id="calculator-form" name="project-form"  enctype="multipart/form-data"  action="" method="post" autocomplete="off">
            <input type="hidden" size="60" readonly value="<?php echo $this->projectData['prj_id'];?>" id="prj_id" name="prj_id" />
            <div id="page_content">
                <div class="pageoverflow">
                    <div class="pagetext">Name:</div>
                    <div class="pageinput">
                        <input type="text" size="60" readonly value="<?php echo $this->projectData['prj_name'];?>" id="prj_name" name="prj_name">
                    </div>
                </div>

                <div class="pageoverflow">
                    <div class="pagetext">Customer:</div>
                    <div class="pageinput">
                        <input type="text" readonly size="60" value="<?php echo $this->customerArray[$this->projectData['cus_id']]; ?>" id="cus_id" name="cus_id" />
                    </div>
                </div>

                <div class="pageoverflow">
                    <div class="pagetext">Designer in charge:</div>
                    <div class="pageinput">
                        <input type="text" readonly size="60" value="<?php echo $this->desginerArray[$this->projectData['dsg_id']]; ?>" id="dsg_id" name="dsg_id" />
                    </div>
                </div>
                
                <div id="calculator-table">
                  
                <div class="pageoverflow" >
                    <p class="pagetext">&nbsp;</p>
                    <div class="pageinput">
                        <div class="table-overflow">
                        <table  style="width: 57%; margin: 0px;" class="pagetable">
                            <tbody>
                                <tr>
                                    <th style="height: 30px;">Job Code</th>
                                    <th style="height: 30px;">UOM</th>
                                    <th style="height: 30px;">Contractor</th>
                                    <th style="height: 30px;">Start date</th>
                                    <th style="height: 30px;">Completion date</th>
                                    <th style="height: 30px;">Estimate Day</th>
                                    <th style="height: 30px;">Quantity</th>
                                    <th style="height: 30px;">Job Remarks</th>
                                </tr>
                                <?php $k = 8000;?>
                            <?php foreach($this->projectReference as $_ref): ?>
                                    <tr class="row2 trid<?php  echo count($this->projectReference)+ $k;?>" trid="<?php  echo count($this->projectReference)+ $k;?>">
                                    <td><?php echo $_ref['job_code'];?></td>
                                    <td><?php echo $_ref['job_uom'];?></td>
                                    <td><?php echo ($this->contractors[$_ref['ctr_id']]['ctr_name']); ?></td>
                                    <td><?php echo $startDate = date('Y-m-d' , strtotime($_ref['job_startdate']));?></td>
                                    <td><?php echo $endDate = date('Y-m-d' , strtotime($_ref['job_enddate']));?></td>
                                    <td>
                                        
                                        <?php 
                                            $currentDate = date('Y-m-d');
                                            $estimateDay = '';
                                            if ( strtotime($currentDate) >= strtotime($startDate) ) {
                                                if ( strtotime($currentDate) >= strtotime($endDate) ) {
                                                    $estimateDay = 0;
                                                } else {
                                                    $estimateDay = GTL_Common::getDateDifference($endDate,$currentDate);
                                                }
                                            } else {
                                                $estimateDay =  "--";
                                            }
                                        ?>
                                        
                                        <?php echo $estimateDay; ?>
                                        
                                        <?php #echo GTL_Common::getDateDifference($endDate,$startDate); ?>
                                        <input type="hidden" class="job_estimate_day_left" value="<?php echo GTL_Common::getDateDifference($endDate, $startDate); ?>"/>
                                    </td>
                                    <td><?php echo $_ref['job_qty'];?></td>
                                    <td><?php echo nl2br($_ref['job_remark']);?></td>
                                </tr>
                                <?php $k++;?>
                            <?php endforeach; ?>
                            </tbody></table>
                        </div>
                    </div>
                </div>
                
                <div class="pageoverflow">
                    <div class="pagetext">Total days for project:</div>
                    <div class="pageinput">
                        <input type="text" readonly size="60" value="<?php echo $this->total_day_diff; ?>" id="prj_totalday" name="prj_totalday" />
                    </div>
                </div>
                
                </div>  
                
                
                
                <?php if (count($this->projectImages) > 0): ?>
                <div class="pageoverflow">
                    <p class="pagetext"> Project Image</p>
                    <div class="pageinput">
                        <div class="project-img">
                            <ul>
                            <?php $projectImageChunk = array_chunk($this->projectImages, 3); ?>
                            <?php foreach ($projectImageChunk as $_prjImages): ?>
                                
                                    <?php foreach ($_prjImages as $_projectImages): ?>
                                    <li>
                                        <?php
                                        $thumb_src_path = $this->PUBLIC_PATH . "upload/project/thumb_" . $_projectImages['prji_image'];
                                        $src_path = $this->PUBLIC_PATH . "upload/project/" . $_projectImages['prji_image'];
                                        ?>
                                        <div style="float:left;">
                                            <a style="text-decoration: none;" class="lightbox" href="<?php echo $src_path; ?>">
                                                <img src="<?php echo $thumb_src_path; ?>" height="150" width="150"/>&nbsp;&nbsp;
                                            </a>
                                        </div>
                                        <div class='clear'></div>
                                        <div style="float:left;">
                                            Type : <b><?php echo $this->imageTypeArray[$_projectImages['prji_type']]; ?></b>
                                        </div>
                                        <div class='clear'></div>
                                        <div style="float:left;">
                                            <?php if ($_projectImages['prji_notes'] != ''): ?>
                                                Notes : <b><?php echo $_projectImages['prji_notes']; ?></b>
                                            <?php else: ?>
                                                &nbsp;
                                            <?php endif; ?>
                                        </div>
                                    </li>
                                    <?php endforeach; ?>
                                
                            <?php endforeach; ?>
                        </ul>
                        </div>
                    </div>
                </div>
                <?php endif; ?>
                
                
                <div class="calculator">
                    <div class="job-material-cnt">
                        <div class="cal-text-cnt">Image Type</div>
                        <select name="imagetype[]" class="type-list">
                            <?php foreach($this->imageTypeArray as $_key => $_image):?>
                                <option value="<?php echo $_key;?>"><?php echo $_image;?></option>
                            <?php endforeach;?>
                        </select>
                    </div>
                    
                    <div class="job-material-cnt">
                        <div class="cal-text-cnt">Notes</div>
                        <input type="text" length="20" name="prji_notes[]">
                    </div>
                    
                    <div class="job-material-cnt">
                        <div class="cal-text-cnt"> Image</div>
                        <input type="file" class="image_class" name="qni_image[]" id="image_id">
                    </div>

                    <div class="job-material-cnt">
                        <div class="cal-text-cnt"> &nbsp;</div>
                        <a href="#" class="add-cnt"><img width="28" height="28" src="<?php echo $this->IMAGE_PATH; ?>icons/system/plus.png" alt="Edit" title="Edit" /></a>
                    </div>
                </div>
                
                
                <div style="clear: both;"></div>
                
                <div class="pageoverflow">
                    <div class="pagetext">Contractor's Project Notes:</div>
                    <div class="pageinput">
                        <textarea rows="1" cols="60" name="project_notes" id="project_notes"></textarea>
                    </div>
                </div>
                
                
                <?php if ( count($this->projectNotes) > 0 ): ?>
                <div class="pageoverflow">
                    <div class="pagetext"><u>Past Project Notes</u></div>
                    <div class="pagetext">
                        <?php foreach($this->projectNotes as $_notes): ?>
                        <div>
                            <div>
                                    <?php if ($_notes['prjn_type'] != '1' && $_notes['prjn_type'] != '3'):?>
                                    <u><?php echo ucfirst(strtolower($this->userRoles[$_notes['prjn_type']])); ?>  </u> : <?php echo ($this->customerArray[$_notes['prjn_lgn_id']]); ?> on <i><?php echo date('jS F, Y h:i a',strtotime($_notes['prjn_createddate']));?></i>
                                    <?php elseif ($_notes['prjn_type'] == '3'):?>
                                    <u> Myself </u> : on <i><?php echo date('jS F, Y h:i a',strtotime($_notes['prjn_createddate']));?></i>
                                    <?php else: ?>
                                    <u>Admin </u> : on <i><?php echo date('jS F, Y h:i a',strtotime($_notes['prjn_createddate']));?></i>
                                    <?php endif;?>
                            </div>
                            <p>
                                <?php echo $_notes['prjn_notes']; ?>
                            </p>
                        </div>
                        <?php endforeach; ?>
                    </div>
                </div>
                <?php endif; ?>
                
                
                <div class="pageoverflow">
                    <p class="pagetext">&nbsp;</p>
                    <p class="pageinput">
                        <input type="submit" title="Save changes" value="Save" id="Save" name="Save">                        
                        <input type="reset" title="Save changes and continue to edit" value="Reset" id="Reset" name="Reset">                    
                    </p>
                </div>
                <div style="clear: both;"></div>
            </div>
        </form>
    </div>
    <p class="pageback">
        <a class="pageback" href="<?php echo $this->url(array('controller' => 'contractor', 'action' => 'project'), null, TRUE) ?>">&#171; Back to Listing</a>
    </p>
    <div class="clearb"></div>
</div>

<div id="calculator-protype" class="display-none">
    <div class="pageoverflow">
        
        <div class="calculator">
            <div class="job-material-cnt">
                <div class="cal-text-cnt">Image Type</div>
                <select name="imagetype[]" class="type-list">
                    <?php foreach ($this->imageTypeArray as $_key => $_image): ?>
                        <option value="<?php echo $_key; ?>"><?php echo $_image; ?></option>
                    <?php endforeach; ?>
                </select>
            </div>

            <div class="job-material-cnt">
                <div class="cal-text-cnt">Notes</div>
                <input type="text" length="20" name="prji_notes[]">
            </div>

            <div class="job-material-cnt">
                <div class="cal-text-cnt"> Image</div>
                <input type="file" class="image_class" name="qni_image[]" id="image_id">
            </div>

            <div class="job-material-cnt">
                <div class="cal-text-cnt"> &nbsp;</div>
                <a href="#" class="add-cnt"><img width="28" height="28" src="<?php echo $this->IMAGE_PATH; ?>icons/system/plus.png" alt="Edit" title="Edit" /></a>
            </div>
            
            <div class="job-material-cnt">
                <div class="cal-text-cnt"> &nbsp;</div>
                <a href="#" class="remove-cnt"><img width="28" height="28" src="<?php echo $this->IMAGE_PATH; ?>icons/system/minus.png" alt="Edit" title="Edit" /></a>
            </div>
            
        </div>
        
    </div>
</div>   
<script>

$(function() {
    
    //Form Validation
    $('#calculator-form').submit(function() {
        
        $('.jscript-error-container').html('');
        //Remove the error class
        $("#calculator-form :input").removeClass('error');

        //Hide Error Container and remove the contents from it
        $('.jscript-error-container').html('');
        $('.jscript-error-container').hide();
        
        //Initialize error container
        var errMessage = [];
        
         //Image validation
        var imageCounter = 0;
        $('#calculator-form').find('.image_class').each(function() {
            if ($(this).val() !== '' ) {
                var ext = $(this).val().split('.').pop().toLowerCase();
                if($.inArray(ext, ['gif','png','jpg','jpeg']) === -1) {
                    $(this).addClass('error');
                    $(this).focus();
                    imageCounter++;
                }

            }
        });
        if (imageCounter > 0) {
            errMessage.push('Please select image with valid extension');
        }
        
           
        if (errMessage.length > 0) {
            var content = "";
            $.each(errMessage, function(index,value){
                content += "<div class='pageoverflow'><label class='errorlabel'>"+value+"</label></div>";
            });
            $('.jscript-error-container').html(content);
            $('.jscript-error-container').show();
            return false;
        } else {
            return true;
        }
            
    });
});
</script>